﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using HelpersDotNet.Serialization;

namespace HelpersDotNet.Extensions
{
    public static partial class Extensions
    {
        public static bool IsSerializable<T>(this T obj)
        {
            return (obj is ISerializable) ||
                (Attribute.IsDefined(typeof(T), typeof(SerializableAttribute)));
        }

        public static Stream ToStream(this byte[] bytes)
        {
            return new MemoryStream(bytes);
        }

        public static byte[] ToBytes(this Stream stream)
        {
            int streamLength = Convert.ToInt32(stream.Length);
            byte[] data = new byte[streamLength];

            stream.Read(data, 0, streamLength);
            stream.Close();

            return data;
        }

        public static byte[] ToSerializedData(this object graph)
        {
            return SerializationHelper.Serialize(graph);
        }

        public static object ToDeserializedData(this byte[] serializedData)
        {
            return SerializationHelper.Deserialize(serializedData);
        }

        public static string ToString(this byte[] bytes, Encoding encoding)
        {
            return encoding.GetString(bytes);
        }

        public static byte[] ToBytes(this string s, Encoding encoding)
        {
            return encoding.GetBytes(s);
        }

        public static Stream ToStream(this string s, Encoding encoding)
        {
            return s.ToBytes(encoding).ToStream();
        }

        public static Stream ToStream(this XmlDocument xdoc)
        {
            MemoryStream ms = new MemoryStream();
            xdoc.Save(ms);
            return ms;
        }

        public static byte[] ToBytes(this XmlDocument xdoc)
        {
            return xdoc.ToStream().ToBytes();
        }


        //public static byte[] ToBytes(this object obj)
        //{
        //    if (obj == null)
        //        return null;

        //    if (!obj.IsSerializable())
        //        throw new SerializationException("The object cannot be serialized");

        //    using (MemoryStream stream = new MemoryStream())
        //    {

        //    }
        //}

        //public static byte[] ToBytes<T>(this T obj)
        //    where T : ISerializable
        //{
        //    using (MemoryStream stream = new MemoryStream())
        //    {
        //        BinaryFormatter formatter = new BinaryFormatter();
        //        formatter.Serialize(stream, obj);
        //        return stream.ToArray();
        //    }
        //}

        //public static object ToObjectFromByteArray(this byte[] buffer)
        //{
        //    using (MemoryStream stream = new MemoryStream(buffer))
        //    {
        //        BinaryFormatter formatter = new BinaryFormatter();
        //        return formatter.Deserialize(stream);
        //    }
        //}

        //public static byte[] ToByteArray(this XmlDocument xdoc)
        //{
        //    using (StringWriter sw = new StringWriter())
        //    {
        //        using (XmlTextWriter xw = new XmlTextWriter(sw))
        //        {
        //            xdoc.WriteTo(xw);
        //            ASCIIEncoding encoding = new ASCIIEncoding();
        //            return encoding.GetBytes(sw.ToString());
        //        }
        //    }
        //}

        //public static byte[] ToByteArray(this string s, Encoding encoding)
        //{
        //    return encoding.GetBytes(s);
        //}

        //public static string ToStringFromByteArray(this byte[] buffer, Encoding encoding)
        //{
        //    char[] c = new char[encoding.GetCharCount(buffer, 0, buffer.Length)];
        //    encoding.GetChars(buffer, 0, buffer.Length, c, 0);
        //    return new string(c);
        //}

    }
}


//    public static byte[] ToByteArray(this Stream stream)
//    {
//        int length = Convert.ToInt32(stream.Length);

//        byte[] data = new byte[length];

//        stream.Read(data, 0, length);
//        stream.Close();

//        return data;
//    }

//    public static byte[] ToByteArray(this decimal d)
//    {
//        using (MemoryStream stream = new MemoryStream())
//        {
//            using (BinaryWriter writer = new BinaryWriter(stream))
//            {
//                writer.Write(d);

//                return stream.ToArray();
//            }
//        }
//    }
//}
//}